package org.example2;

import java.util.concurrent.TimeUnit;

public class AppMain {
    public static void main(String[] args) throws Exception {

        AppMain appMain = new AppMain();

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("子线程---启动了");
                System.out.println("子线程---锁对象appMain:" + System.identityHashCode(appMain));
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("子线程---睡醒了1");
                try {
                    TimeUnit.SECONDS.sleep(30);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("子线程---睡醒了2");
                synchronized (appMain) {
                    System.out.println("子线程---进入synchronized代码块了--notifyAll前1");
                    System.out.println("子线程---进入synchronized代码块了--notifyAll前2");
                    appMain.notifyAll();
                    System.out.println("子线程---进入synchronized代码块了--notifyAll后1");
                    System.out.println("子线程---进入synchronized代码块了--notifyAll后2");
                }
                System.out.println("子线程---唤醒所有对象");
            }
        },"子线程").start();

        TimeUnit.MILLISECONDS.sleep(10);
        appMain.show();
        System.out.println("main方法结束了");
    }

    public void show() {
        System.out.println("main线程进入show了");
        System.out.println("main线程show中的锁对象this:" + System.identityHashCode(this));
        synchronized (this) {
            try {
                System.out.println("main线程进入synchronized代码块了--wait前1");
                System.out.println("main线程进入synchronized代码块了--wait前2");
                this.wait();
                System.out.println("main线程进入synchronized代码块了--wait后1");
                System.out.println("main线程进入synchronized代码块了--wait后2");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                System.out.println("main线程进入finally了,马上出show方法了");
            }
        }
    }
}
